import UserModel from "../schema/user";
import { Request, Response } from "express";
import Types from "../typings";

// Common 类的实例
import Common from "./common";
const UserCommonIns = new Common(UserModel);

// 响应类型实例
import ResType from "./resType";
const ResTypeIns = new ResType();

class User {
  constructor() {}

  // 查看用户列表
  async list(req: Request, res: Response) {
    try {
      const creatorId = UserCommonIns.getLoginUserId(
        req as Types.Express.ConvertRequest
      );
      let { page = 1, size = 10, name = "" } = req.query;
      page = Number(page);
      size = Number(size);

      const query: { creatorId: string; name?: any } = {
        creatorId,
      };

      if (name) {
        query.name = { $regex: name, $options: "i" };
      }

      const total = await UserCommonIns.Count(query);
      const users: Types.User.UserType[] = await UserCommonIns.Find(query)
        .skip(size * (page - 1))
        .limit(size)
        .sort({
          createdTime: -1,
        });

      const data = {
        page,
        size,
        list: users,
        total,
      };

      ResTypeIns.Success(res, data);
    } catch (err) {
      ResTypeIns.Database(res, err);
    }
  }

  // 查看单个用户
  async check(req: Request, res: Response) {
    try {
      const userId = UserCommonIns.getLoginUserId(
        req as Types.Express.ConvertRequest
      );
      let { id } = req.query;

      if (!id && !userId) {
        ResTypeIns.paramsWrong(res);
      } else {
        // 查询用户数据
        const users: Types.User.UserType[] = await UserCommonIns.Find({
          id: id || userId,
        });

        if (!users.length) {
          // 用户不存在
          ResTypeIns.isNotExist(res);
        } else {
          // 用户存在时
          ResTypeIns.Success(res, users[0]);
        }
      }
    } catch (err) {
      ResTypeIns.Database(res, err);
    }
  }

  // 新增用户
  async add(req: Request, res: Response) {
    try {
      const creatorId = UserCommonIns.getLoginUserId(
        req as Types.Express.ConvertRequest
      );

      const { account, password, name, gender, roleId, avatar, desc } =
        req.body;

      const users: Types.User.UserType[] = await UserCommonIns.Find({
        account,
      });

      if (users.length) {
        ResTypeIns.isExist(res);
      } else {
        await UserCommonIns.SaveOne({
          account,
          password,
          name,
          gender,
          roleId,
          avatar,
          desc,
          creatorId,
          createdTime: Date.now(),
          updatedTime: Date.now(),
        });

        ResTypeIns.Success(res);
      }
    } catch (err) {
      ResTypeIns.Database(res, err);
    }
  }

  // 更新用户信息
  async update(req: Request, res: Response) {
    try {
      const { id, password, name, gender, roleId, avatar, desc } = <
        Partial<Types.User.UserType>
      >req.body;
      if (!id) {
        ResTypeIns.paramsWrong(res);
      } else {
        const updateContent = {
          password,
          name,
          gender,
          roleId,
          avatar,
          desc,
          updatedTime: Date.now(),
        };

        await UserCommonIns.UpdateOne({ id }, updateContent);
        ResTypeIns.Success(res);
      }
    } catch (err) {
      ResTypeIns.Database(res, err);
    }
  }

  // 删除单个用户
  async remove(req: Request, res: Response) {
    try {
      let { id } = req.query;

      if (!id) {
        ResTypeIns.paramsWrong(res);
      } else {
        // 删除用户数据
        await UserCommonIns.RemoveOne({ id });

        ResTypeIns.Success(res);
      }
    } catch (err) {
      ResTypeIns.Database(res, err);
    }
  }
}

export default User;
